package net.entelijan.cobean.data.literature;

import java.util.List;

import javax.jws.WebService;


/**
 * Service layer for managing a simple book database
 *
 */
@WebService
public interface ILitService {
	
	/**
	 * Adds a new book to the database if the books id is <= 0
	 * or updates the properties of an existing book if they where changed.
	 * @param book The book to be saved
	 */
	public void saveBook(LitBook book);
	
	/**
	 * Deletes a book from the database. Related authors
	 * are not deleted
	 * @param book The book to be deleted
	 */
	public void deleteBook(long id);

	/**
	 * Adds a new author to the database if the authors id is <= 0
	 * or updates the properties of an existing author if they where changed.
	 * @param book The author to be saved
	 */
	public void saveAuthor(LitAuthor author);
	
	/**
	 * Deletes an author from the database. Related Books are not deleted.
	 * @param author The author to be deleted
	 */
	public void deleteAuthor(long id);

	/**
	 * Adds a book to an author. 
	 * The author and the book must be persisted before
	 */
	public void addBookToAuthor(LitAuthor author, LitBook book);
	
	/**
	 * Removes the relationship between an author and a book.
	 * The author and the book must be persisted before
	 */
	public void removeBookFromAuthor(LitAuthor author, LitBook book);
	
	/**
	 * Loads a list of books. The reference to the books
	 * author must not be initialized. Use loadBook to
	 * get the reference to the books author.
	 * 
	 * @return a list of books
	 */
	public List<LitBook> loadAllBooks();

	/**
	 * Loads a list of authors. The 'books' property must not be initialized.
	 * To get the authors books use loadBooks.
	 * 
	 * The authors returned by that method have no books attached. So never
	 * use an object loaded with this method for update. Otherwise all
	 * assigned books will be lost.
	 * 
	 * If you need to edit an existing author, load it with loadAuthor(id) first.
	 * 
	 * @return list of books
	 */
	public List<LitAuthor> loadAllAuthors();

	/**
	 * Loads one author by id
	 * @param id
	 * @return The author if it exists. null otherwise
	 */
	public LitAuthor loadAuthor(long id);

	/**
	 * Loads a book by id
	 * @param id
	 * @return The book if it exists. null otherwise
	 */
	public LitBook loadBook(long id);
	
	/**
	 * Searches books using the attributes of the 
	 * search items.
	 * @param searchItem The search item containing the search parameters. May not be null
	 * @return List of the found books
	 */
	public List<LitBook> findBooks(LitBookSearchItem searchItem);

	/**
	 * Returns a list of books from the 'fromIndex' to the 'toIndex'
	 * Validations
	 * - The fromIndex must not be greater than the to index
	 * - The toIndex must not be greater than the value returned fom getBookCount()
	 * - The list of order items must not be empty
	 * 
	 * @param fromIndex Start Index 
	 * @param toIndex End Index
	 * @param order A list of order items that define the order of the returned books.
	 * @return The found books.
	 */
	public List<LitBook> loadBooks(int fromIndex, int toIndex, List<OrderAttribute> order);

	/**
	 * Returns the number of books
	 * @return The number of books
	 */
	public long getBookCount();
}
